package entities;

import static javax.persistence.CascadeType.ALL;

import java.io.Serializable;
import java.util.Date;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import enums.TournamentStatus;

@Entity
@Table(name="tournament")
@NamedQueries({
	@NamedQuery(name="getListTournamentByFlagActivated",query="SELECT tournament FROM Tournament AS tournament WHERE tournament.oneFlag = :flag"),
	@NamedQuery(name="getTournamentByID",query="SELECT tournament FROM Tournament AS tournament WHERE tournament.id = :id"),
	@NamedQuery(name="getListTournamentByFlagActivatedByTournamentStatus",query="SELECT tournament FROM Tournament AS tournament WHERE tournament.oneFlag = :flag AND tournament.status = :status")
})
public class Tournament extends EntityMother implements Serializable {

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy= GenerationType.IDENTITY)
	private long id;

	private String name;

	@Temporal(TemporalType.DATE)
	private Date startDate;

	private String place;

	@OneToMany(fetch = FetchType.EAGER, cascade = ALL, mappedBy = "tournament")
	private Set<Team> listTeams;

	@OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.ALL} , mappedBy = "tournament")
	private Set<Referee> listReferees;

	@OneToMany(fetch = FetchType.EAGER, cascade = ALL, mappedBy = "tournament")
	@OrderBy("roundNumber")
	private Set<Match> listMatchs;

	private int pauseDuration;

	private int halfTimeDuration;

	private int prolongationDuration;

	@Enumerated(EnumType.STRING)
	private TournamentStatus status;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public String getPlace() {
		return place;
	}

	public void setPlace(String place) {
		this.place = place;
	}

	public Set<Team> getListTeams() {
		return listTeams;
	}

	public void setListTeams(Set<Team> listTeams) {
		this.listTeams = listTeams;
	}

	public Set<Referee> getListReferees() {
		return listReferees;
	}

	public void setListReferees(Set<Referee> listReferees) {
		this.listReferees = listReferees;
	}

	public int getPauseDuration() {
		return pauseDuration;
	}

	public void setPauseDuration(int pauseDuration) {
		this.pauseDuration = pauseDuration;
	}

	public int getHalfTimeDuration() {
		return halfTimeDuration;
	}

	public void setHalfTimeDuration(int halfTimeDuration) {
		this.halfTimeDuration = halfTimeDuration;
	}

	public int getProlongationDuration() {
		return prolongationDuration;
	}

	public void setProlongationDuration(int prolongationDuration) {
		this.prolongationDuration = prolongationDuration;
	}

	public TournamentStatus getStatus() {
		return status;
	}

	public void setStatus(TournamentStatus status) {
		this.status = status;
	}

	public Set<Match> getListMatchs() {
		return listMatchs;
	}

	public void setListMatchs(Set<Match> listMatchs) {
		this.listMatchs = listMatchs;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + halfTimeDuration;
		result = prime * result + (int) (id ^ (id >>> 32));
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + pauseDuration;
		result = prime * result + ((place == null) ? 0 : place.hashCode());
		result = prime * result + prolongationDuration;
		result = prime * result
				+ ((startDate == null) ? 0 : startDate.hashCode());
		result = prime * result + ((status == null) ? 0 : status.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		Tournament other = (Tournament) obj;
		if (halfTimeDuration != other.halfTimeDuration)
			return false;
		if (id != other.id)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (pauseDuration != other.pauseDuration)
			return false;
		if (place == null) {
			if (other.place != null)
				return false;
		} else if (!place.equals(other.place))
			return false;
		if (prolongationDuration != other.prolongationDuration)
			return false;
		if (startDate == null) {
			if (other.startDate != null)
				return false;
		} else if (!startDate.equals(other.startDate))
			return false;
		if (status != other.status)
			return false;
		return true;
	}
}